﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Ocmf.Types
{
    public class ScreenTemplate: Panel 
    {
        public string DirectoryPath { get { return Path.GetDirectoryName(FilePath); } }
        public string FileName { get { return Path.GetFileName(FilePath); } }
        public string FilePath { get; set; }
        public DateTime LastSaved { get; set; }
        public string AspectRatio { get; set; }
        public int GridSize { get; set; }

        public ScreenTemplate()
        {
            AspectRatio = "4:3";
            GridSize = 10;
            BackColor= Color.White;
        }

        public void Save(string path)
        {
            //set filename property
            FilePath = path;

            //create xml document and file declaration
            var xmlDocument = new XmlDocument();
            var xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            xmlDocument.AppendChild(xmlDeclaration);

            //create the root element
            var screenTemplateElement = xmlDocument.CreateElement("ScreenTemplate");

            //create, set and add the application version
            var versionAttribute = xmlDocument.CreateAttribute("OcmfVersion");
            versionAttribute.InnerText = Application.ProductVersion;
            screenTemplateElement.Attributes.Append(versionAttribute);

            //set the aspect ratio attribute 
            var aspectRatioAttribute = xmlDocument.CreateAttribute("AspectRatio");
            aspectRatioAttribute.InnerText = AspectRatio;
            screenTemplateElement.Attributes.Append(aspectRatioAttribute);

            //create, set and add the grid size attribute
            var gridSizeAttribute = xmlDocument.CreateAttribute("GridSize");
            gridSizeAttribute.InnerText = GridSize + " px";
            screenTemplateElement.Attributes.Append(gridSizeAttribute);

            //create,set and add the backcolor attribute
            var backColourAttribute = xmlDocument.CreateAttribute("BackColour");
            backColourAttribute.InnerText = BackColor.ToArgb().ToString();
            screenTemplateElement.Attributes.Append(backColourAttribute);

            for(var i = 0; i < Controls.Count; i++)
            {
                var placeholderElement = xmlDocument.CreateElement("ImagePlaceholder");

                var xAttribute = xmlDocument.CreateAttribute("X");
                xAttribute.InnerText = ( Controls[i].Left/ (float)Width).ToString();
                placeholderElement.Attributes.Append(xAttribute);

                var yAttribute = xmlDocument.CreateAttribute("Y");
                yAttribute.InnerText = (Controls[i].Top/(float)Height).ToString();
                placeholderElement.Attributes.Append(yAttribute);

                var widthAttribute = xmlDocument.CreateAttribute("Width");
                widthAttribute.InnerText = (Controls[i].Width/(float)Width ).ToString();
                placeholderElement.Attributes.Append(widthAttribute);

                var heightAttribute = xmlDocument.CreateAttribute("Height");
                heightAttribute.InnerText = (Controls[i].Height/(float)Height).ToString();
                placeholderElement.Attributes.Append(heightAttribute);

                screenTemplateElement.AppendChild(placeholderElement);
            }

            xmlDocument.AppendChild(screenTemplateElement);
            xmlDocument.Save(FilePath);
        }

        public void Open(string path)
        {
            Controls.Clear();

            FilePath = path;
            
            var xmlDocument = new XmlDocument();
            xmlDocument.Load(path);

            BackColor = Color.FromArgb(int.Parse(xmlDocument.DocumentElement.Attributes["BackColour"].InnerText));
            GridSize = int.Parse(xmlDocument.DocumentElement.Attributes["GridSize"].InnerText.Replace(" px",""));
            AspectRatio = xmlDocument.DocumentElement.Attributes["AspectRatio"].InnerText;

            foreach (XmlElement placeHolder in xmlDocument.DocumentElement.ChildNodes)
            {
                var left = float.Parse(placeHolder.Attributes["X"].InnerText);
                var top = float.Parse(placeHolder.Attributes["Y"].InnerText);
                var width = float.Parse(placeHolder.Attributes["Width"].InnerText);
                var height = float.Parse(placeHolder.Attributes["Height"].InnerText);

                Controls.Add(new ImagePlaceholder()
                                 {
                                     Left = (int)(left * Width),
                                     Top = (int)(top * Height),
                                     Width = (int)(width * Width),
                                     Height = (int)(height * Height)
                                 });
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var gfx = e.Graphics;

            gfx.Clear(BackColor);

            for (var i = 0; i < Width; i += GridSize)
                gfx.DrawLine(Pens.Silver, i, 0, i, Height);

            for (var i = 0; i < Height; i += GridSize)
                gfx.DrawLine(Pens.Silver, 0, i, Width, i);

        }

        public static ScreenTemplate LoadProperties(string path)
        {
            return new ScreenTemplate
                       {
                           FilePath = path,
                           LastSaved = File.GetLastWriteTime(path)
                       };
        }

        public void Save()
        {
            Save(FilePath);
        }
    }
}
